(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('alertMemoController', alertMemoController);
    alertMemoController.$inject = ['$scope', '$filter', 'AlertMemoService', 'searchService', 'AlertTypeService'];

    function alertMemoController($scope, $filter, AlertMemo, searchService, AlertTypeService) {

        $scope.condition = {
            alertName: '',
            partitionId: '',
            moduleName: '',
            userNo: '',
            msgType: null,
            beginDateTime: getToday00Clock(),
            endDateTime: getToday11Clock()
        };

        var collapseAllGroups = function (grid) {
            grid.table.find(".k-grouping-row").each(function () {
                grid.collapseGroup(this);
            });
        };

        $scope.dataBound =  function (e) {
            collapseAllGroups($scope.grid);
        };


        $scope.sendTypeData = [
            {value: '0', text: '邮件'},
            {value: '1', text: '短信'},
            {value: '2', text: '邮件&短信'}
        ];

        searchService.getPartition({cname: null, fname: null, ename: null}).then(function (resp) {
            $scope.partitionData = resp.data;
        }, function (error) {
            notify('error', '失败', '查询分区失败，' + error);
        });

        $scope.partitionHeaderTmp = kendo.template($("#partition-header-template").html());
        $scope.partitionTmp = kendo.template($("#partition-template").html());

        AlertTypeService.query({}).then(function (resp) {
            $scope.alertTypeData = resp;
        }, function (error) {
            notify('error', '失败', '查询报警类型失败，' + error);
        });

        $scope.alertTypeHeaderTmp = kendo.template($("#alertType-header-template").html());
        $scope.alertTypeTmp = kendo.template($("#alertType-template").html());

        $scope._initPage = true;

        $scope.pageable = {
            refresh: true,
            pageSizes: true,
            numeric: false,
            input: true
        };
        $scope.Model = kendo.data.Model.define({
            fields: {
                alertName: {type: "string"},
                partitionId: {type: "string"},
                moduleName: {type: "string"},
                userNo: {type: "string"},
                userName: {type: "string"},
                msgType: {type: "string"},
                recCreateTime: {type: "date"},
                alertMsg: {type: 'string'}

            }
        });
        $scope.columns = [
            {
                field: "alertMsg", title: "报警消息", width: 280, hidden: true
            },
            {
                field: "alertName", title: "报警类型", width: 150
            }, {
                field: "partitionId", title: "分区", width: 150
            }, {
                field: "moduleName", title: "功能名", width: 150
            }, {
                field: "userNo", title: "工号", width: 150
            }, {
                field: "userName", title: "姓名", width: 150
            }, {
                field: "msgType", title: "发送方式", width: 150,
                template: function (dataItem) {
                    var desc = '';
                    if ($scope.sendTypeData) {
                        angular.forEach($scope.sendTypeData, function (item, i) {
                            if (item.value == dataItem.msgType) {
                                desc = item.text;
                            }
                        });
                    }
                    return desc == "" ? dataItem.msgType : desc;
                }
            },
            {
                field: "recCreateTime", title: "记录时间", width: 280, format: "{0: yyyy-MM-dd HH:mm:ss}"
            }
        ];

        $scope.dataSource = new kendo.data.DataSource({
            autoBind: false,
            transport: {
                read: function (e) {
                    AlertMemo.query($scope.condition).then(function (response) {
                        e.success(response);
                    }, function (error) {
                        e.error(error);
                    });
                },

                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            requestStart: function (e) {
                if ($scope._initPage) {
                    e.preventDefault();
                }
            },
            group: [
                {field: "alertMsg", dir: "desc"}
            ],
            sortable: true,
            batch: true,
            pageSize: 15,
            schema: {
                model: $scope.Model
            }
        });

        $scope.query = function () {
            $scope._initPage = false;
            $scope.grid.dataSource.read();
        };
    }
})();
